# Esta clase permite menejar los Trainees. Estos se diferencian de los ogxtrainees, ya que son trainees del exterior, que son candidatos a realizar una experiencia en nuestro país. 

class TraineesController < ApplicationController

	before_filter :ensure_login
	before_filter :get_mod, :only => [:new,:edit, :delete]

# Recupera el historial de un trainee para ser visualizado en pantalla.
	def historial
		@log_trainees = LogTrainee.search(params[:search],params[:page],params[:trainee_id])
		@trainee = Trainee.find_by_id(params[:trainee_id])
		@titulo = @trainee.nombre
		respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @log_trainees }
    end
	end

# Recupera todos los trainees.
  def index
   if params[:estado_id] then	
   	  @trainee = Trainee.search_estado(params[:page],params[:estado_id])
   else
   	 if params[:cv] then   	 
   	 		@trainee = Trainee.search_cv(params[:page],params[:cv])
   	 else
   	 		@trainee = Trainee.search(params[:page])  	 
   	 end
	 end
    respond_to do |format|
     	format.js { render :partial => 'trainee_table'  }
      format.html # index.html.erb
    end
   
  end

# Recupera el trainee que va a ser mostrado en pantalla.
  def show
    @trainee = Trainee.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @trainee }
    end
  end

# Prepara el trainee que va a ser posteriormente creado.
  def new
    
    @trainee = Trainee.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @trainee }
    end
  end

# Recupera el trainee que va a ser editado.
  def edit
    @trainee = Trainee.find(params[:id])
  end

# Crea el trainee.
  def create
    @trainee = Trainee.new(params[:trainee])
    respond_to do |format|
      if @trainee.save
				format.js
        format.html { redirect_to(trainees_path) }
        format.xml  { render :xml => @trainee, :status => :created, :location => @trainee }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @trainee.errors, :status => :unprocessable_entity }
      end
    end
  end

# Actualiza el trainee.
  def update
    @trainee = Trainee.find(params[:id])

    respond_to do |format|
      if @trainee.update_attributes(params[:trainee])
      
      	@log_trainee = LogTrainee.new
    		@log_trainee.fecha_ingreso = Date.today
    		@log_trainee.trainee = @trainee
    		@log_trainee.descripcion = 'Actualizacion de datos del trainee'
    		@log_trainee.save        
      
        flash[:notice] = 'trainee was successfully updated.'
        format.html { redirect_to(trainees_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @trainee.errors, :status => :unprocessable_entity }
      end
    end
  end

# Elimina el trainee.
  def destroy
    @trainee = Trainee.find(params[:id])
    @trainee.destroy

    respond_to do |format|
      format.html { redirect_to(trainees_path) }
      format.xml  { head :ok }
    end
  end
  
# Realiza busquedas por tags del cv ingresado.
 	def buscar
		target = params[:search].split(' ')
		@curriculums = Curriculum.find(:all)
		trainees_id = []
		for curr in @curriculums
			if curr.has_tags(target) then
				trainees_id.push(curr.trainee_id)
			end
		end

		@trainee = Trainee.search_individuals(params[:page],trainees_id)
		
		respond_to do |format|
			format.js
			format.html { render :action => 'index' }
		end
	end
  
  
end
